﻿using HPIT.RentHouse.Common;
using RentHouse.DTO;
using RentHouse.IService;
using RentHouse.Service.Entityes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RentHouse.Service
{
    public class PermissionService : IPermissionService
    {
        public PermissionDTO Get(long id)
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            var model = baseService.Get(e => e.Id == id);
            PermissionDTO permissionDTO = new PermissionDTO()
            {
                Id = model.Id,
                Name = model.Name,
                Description = model.Description,
            };
            return permissionDTO;
        }

        public List<PermissionDTO> GetList()
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            List<PermissionDTO> list = baseService.GetList(e => true).Select(e => new PermissionDTO() {
                Id = e.Id,
                Name = e.Name,
                Description = e.Description
            }).ToList();
            return list;
        }

        #region 列表
        public List<PermissionDTO> GetPagedList(int start, int length, string name, ref int count)
        {
            using (var db = new RentHouseEntity())
            {
                //RentHouseEntity db = new RentHouseEntity();
                //BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                var bs = new BaseService<T_Permissions>(db);
                var query = PredicateExtensions.True<T_Permissions>();
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.And(m => m.Name.Contains(name));
                }
                //List<T_Permissions> list = baseService.GetPagedList(start, length, ref count, query, a => a.Id).ToList();
                var list = bs.GetPagedList(start, length, ref count, query, m => m.Id, false).Select(m=> new PermissionDTO
                //return list.Select(m => new PermissionDTO()
                {
                    Id = m.Id,
                    Name = m.Name,
                    Description = m.Description
                }).ToList();
                return list;
            }
        }
        #endregion

        #region 添加
        /// <summary>
        /// 添加功能代码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Add(PermissionDTO dto)
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            T_Permissions per = new T_Permissions()
            {
                CreateDateTime = DateTime.Now,
                Description = dto.Description,
                Name = dto.Name,
                IsDeleted = false
            };
            long Id = baseService.Add(per);
            if (Id > 0)
            {
                return new AjaxResult(ResultState.Success, "添加成功");
            }
            else
            {
                return new AjaxResult(ResultState.Error, "添加失败");
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 编辑功能加载页面代码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public PermissionDTO  GetPermission(long Id)
        {
            using(var db= new RentHouseEntity())
            {
                var bs = new BaseService<T_Permissions>(db);
                var model = bs.Get(e => e.Id == Id);
                if (model != null)
                {
                    var dto = new PermissionDTO()
                    {
                        Id = model.Id,
                        Description=model.Description,
                        Name=model.Name
                    };
                    return dto;
                }
                return null;
            }
        }
        #endregion

        #region 修改
        /// <summary>
        /// 编辑功能提交页面代码
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public AjaxResult Edit(PermissionDTO dto)
        {
            using(var db=new RentHouseEntity())
            {
                var bs = new BaseService<T_Permissions>(db);
                //实例化权限实体
                var model = bs.Get(e => e.Id == dto.Id);
                if (model != null)
                {
                    model.Name = dto.Name;
                    model.Description = dto.Description;
                    bs.Update(model);
                    return new AjaxResult(ResultState.Success, "编辑成功");
                }
                return new AjaxResult(ResultState.Error, "您要编辑的信息不存在");
            }
        }
        #endregion

        #region 单个删除
        public AjaxResult Delete(long Id)
        {
            using (var db=new RentHouseEntity())
            {
                var bs = new BaseService<T_Permissions>(db);
                //实例化权限实体
                var model = bs.Get(e => e.Id == Id);
                if (model != null)
                {
                    //执行删除
                    bs.Delete(model);
                    return new AjaxResult(ResultState.Success, "删除成功");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "该权限已经被删除");
                }
            }
        }
        #endregion

        #region 批量删除
        public AjaxResult DeleteBatch(List<long> ids)
        {
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            try
            {
                foreach (long Id in ids)
                {
                    T_Permissions per = baseService.Get(e => e.Id == Id);
                    bool flag = baseService.Delete(per);
                    
                }
                return new AjaxResult(ResultState.Success, "删除成功");
            }
            catch (Exception ex)
            {
                return new AjaxResult(ResultState.Error, "删除失败");
            }
        }
        #endregion

        #region 获取用户权限
        public List<PermissionDTO> GetListByUser(long userId)
        {
            List<PermissionDTO> perList = new List<PermissionDTO>();
            RentHouseEntity db = new RentHouseEntity();
            BaseService<T_AdminUsers> baseService = new BaseService<T_AdminUsers>(db);
            T_AdminUsers user = baseService.Get(e => e.Id == userId);
            List<T_Roles> roleList = user.T_Roles.ToList();
            foreach (T_Roles role in roleList)
            {
                List<PermissionDTO> permissions = role.T_Permissions.Select(e => new PermissionDTO()
                {
                    Id = e.Id,
                    Description = e.Description,
                    Name = e.Name
                }).ToList();
                perList.AddRange(permissions);
            }
            return perList;
        }
        #endregion
    }
}
